<?php
/**
 * @version 	$Id$
 * @author 		Luu Trong Hieu <tronghieu1012@yahoo.com>
 * @copyright 	Copyright (c) 2010 Luu Trong Hieu. All right reseverd.
 * @license 	GNU/GPL see license.php
 */
defined('IN_FLYWHEEL') or die('Restricted Access');

/**
 * Flywheel System Autoload
 * 
 * @package 	system
 * @subpackage 	autoload
 */
class fSystemAutoload {
	//Map of classes
	protected $map = array();
	
	public function __construct() {
		require_once dirname(__FILE__)._DS_.'coreMapper.php';		
	}
	
	public static function getInstance() {
		static $instance;
		if ($instance == null) {
			$instance = new fSystemAutoload();
		}
		
		return $instance;		
	}
	/**
	 * Register fAutoload in spl autoloader
	 * 
	 * @return void
	 */
	public static function register() {
		ini_set('unserialize_callback_func', 'spl_autoload_call');
		if (false === spl_autoload_register(array(self::getInstance(), 'autoload')))
	    {
	      throw new fException(sprintf('Unable to register %s::autoload as an autoloading method.', get_class(self::getInstance())));
	    }
	}
	
	/**
	 * Load Class
	 *
	 * @param string $class class name
	 * @return boolean if load success
	 */
	public function autoload($class) {
		if (class_exists($class, false) || interface_exists($class, false)) {
			return true;
		}
		$class = strtolower($class);
		if ($class == 'ffolderhelper') {
			require $this->map[$class];
			return;
		}
		
		if (isset($this->map[$class]) && file_exists($this->map[$class])) {
			require $this->map[$class];
			return true;
		}
		
		return false;
	}
}